/**
 * chenuu.com
 * 
 * @author  zhaoxu.com@gmail.com
 * @created 2012-11-10
 */
package com.chenuu.integration.httpclient.dropbox;

import java.util.Date;
import java.util.Map;

import com.dropbox.client2.RESTUtility;

/**
 *
 */
public class DropboxLink {
	
	/** The url it links to */
    public final String url;
    /** When the url expires (after which this link will no longer work) */
    public final Date expires;

    private DropboxLink(String returl, boolean secure) {
        if (!secure && returl.startsWith("https://")) {
            returl = returl.replaceFirst("https://", "http://");
            returl = returl.replaceFirst(":443/", "/");
        }
        url = returl;
        expires = null;
    }

    public DropboxLink(Map<String, Object> map) {
        this(map, true);
    }

    /**
     * Creates a DropboxLink, with security optionally set to false.
     * This is useful for some clients, such as Android, which use
     * this to play a streaming audio or video file, and which are
     * unable to play from streaming https links.
     *
     * @param map the parsed parameters returned from Dropbox
     * @param secure if false, returns an http link
     */
    public DropboxLink(Map<String, Object> map, boolean secure) {
        String returl = (String)map.get("url");
        String exp = (String)map.get("expires");
        if (exp != null) {
            expires = RESTUtility.parseDate(exp);
        } else {
            expires = null;
        }

        if (!secure && returl.startsWith("https://")) {
            returl = returl.replaceFirst("https://", "http://");
            returl = returl.replaceFirst(":443/", "/");
        }
        url = returl;
    }
}
